// Methods of Color class



#include "Color.h"



Color :: Color(Color & another){

     freeColor(); 
     (*this) = another;
}


Color :: Color(const MyDisplay &another, short red, short green, short blue){

     show = new MyDisplay(another);   
     
   screenColormap = DefaultColormap(show->display , show->screenNo);  
     
   setColor(red,green,blue); 
   
}

Color :: Color(const MyDisplay &another){

     show = new MyDisplay(another); 
 
}

Color :: ~Color(){

   freeColor();
}


unsigned long Color :: pixel(){

  return screenColor.pixel;
}


unsigned short Color :: red(){
 
       return screenColor.red * 255 / 65535;
}


unsigned short Color :: blue(){

       return screenColor.blue * 255 / 65535;

}



unsigned short Color :: green(){

       return screenColor.blue * 255 / 65535;

}


void Color :: setBackgroundColor(short red ,short green ,short blue ){

         setColor(red,green,blue) ;

	 
         if(! XSetBackground(show->display ,show->gc , pixel())){
	 
	   cout << "cannot allocate background color";
	   getchar();
	 }
    
          //XDrawLine(show->display, show->win, show->gc, 50, 0, 50, 200);
}


void Color :: setBackgroundColor(Color &another){


         if(! XSetBackground(show->display , show->gc ,another.pixel() )){
	 
	   cout << "cannot allocate background color";
	   getchar();
	 }
    
}

void Color :: setForegroundColor(short red ,short green ,short blue ){


          setColor(red,green,blue);           

         if(! XSetForeground(show->display , show->gc  , pixel() )){
	 
	   cout << "cannot allocate foreground color";
	   getchar();
	 }
	 
	// XDrawLine(show->display, show->win, show->gc, 50, 0, 50, 200);
}


void Color :: setForegroundColor(Color &another){

         if(! XSetForeground(show->display ,show->gc ,another.pixel() )){
	 
	   cout << "cannot allocate foreground color";
	   getchar();
	 }
}

Color Color :: getBackgroundColor(){

    // return ()
    return *this;
}


Color Color :: getForegroundColor(){

  return *this;
}



void Color :: setColor(short red, short green, short blue){

         //Status rc;

         screenColor.red = red * 65535 / 255;
	 screenColor.green = green * 65535 / 255;
	 screenColor.blue = blue * 65535 / 255;
	 screenColor.flags = DoRed | DoGreen | DoBlue;

	 /*
	 rc = XAllocNamedColor(show->display, screenColormap, "screenColor", &screenColor, &screenColor);
          if (rc == 0) {
             fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
             exit(1);
          }
	 
	 */
	 
	  if ( ! XAllocColor ( show->display, screenColormap, &screenColor ) )
	    {
	      cout<<"Could not create the color."<<endl;
	      throw  "Could not create the color." ;
	    }

	    
}


void Color :: freeColor(){

          unsigned long pixels[2];
	  pixels[0] = pixel();

 	  XFreeColors ( show->display,
			screenColormap,
			pixels,
			1,
			0 );

}



